

local dingjian = fk.CreateSkill{
  name = "wk_heg__dingjian",
}

Fk:loadTranslationTable{
  ["wk_heg__dingjian"] = "定谏",
  [":wk_heg__dingjian"] = "出牌阶段，你可展示牌堆顶一张牌并选择一项：1.弃置半数手牌（向上取整），然后使用其中一张与展示牌花色相同的牌；2.获得此牌，然后你本回合不能选择此项。",

  ["#wk_heg__dingjian-use"] = "定谏：你可以使用其中一张牌",
  ["@wk_heg__dingjian-turn"] = "定谏",
  ["dingjian"] = "定谏",

  ["wk_heg__dingjian_discard"] = "弃牌，使用其中一张",
  ["wk_heg__dingjian_forbidden"] = "获得牌，不能再选择此项",

  ["$wk_heg__dingjian1"] = "礼尚往来，乃君子风范。",
  ["$wk_heg__dingjian2"] = "以子之矛，攻子之盾。",
}


dingjian:addEffect("active", {
  anim_type = "offensive",
  can_use = function(self, player)
    return not player:isKongcheng()
  end,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  card_num = 0,
  target_num = 0,
  on_use = function(self, room, effect)
    local player = effect.from
    local get = room:getNCards(1)
    local card_get = Fk:getCardById(get[1])
    player:showCards(get)
    local old_mark = player:getMark("@wk_heg__dingjian-turn")
    if old_mark == 0 then old_mark = {} end
    local choices = {"wk_heg__dingjian_discard", "wk_heg__dingjian_forbidden"}
    if table.contains(old_mark, card_get:getSuitString(true)) or player:getMark("wk_heg__dingjian_forbidden_none-turn") > 0 then
      choices = {"wk_heg__dingjian_discard"}
    end
    local choice = room:askToChoice(player, {choices = choices, skill_name = dingjian.name})
    if choice == "wk_heg__dingjian_forbidden" then
      room:moveCards({
        ids = get,
        to = player,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonJustMove,
        skillName = dingjian.name,
        proposer = player,
      })
      room:setPlayerMark(player, "wk_heg__dingjian_forbidden_none-turn", 1)
    else
      room:moveCards({
        ids = get,
        toArea = Card.DrawPile,
        moveReason = fk.ReasonJustMove,
        skillName = dingjian.name,
      })
      local n = (player:getHandcardNum() + 1) // 2
      local cards = room:askToDiscard(player, {
        min_num = n,
        max_num = n,
        include_equip = false,
        skill_name = dingjian.name,
        cancelable = false,
      })
      local to_use = {}
      to_use = table.filter(cards, function (id)
        local card = Fk:getCardById(id)
        return room:getCardArea(id) == Card.DiscardPile and not player:prohibitUse(card) and card.suit == card_get.suit
      end)
      local use = room:askToUseRealCard(player, {
        pattern = to_use,
        skill_name = dingjian.name,
        prompt = "#wk_heg__dingjian-use",
        extra_data = {
          bypass_times = true,
          extraUse = true,
          expand_pile = to_use,
        },
        cancelable = true,
        skip = true,
      })
      if use then
        room:useCard(use)
      end
    end
  end,
})

return dingjian
